package main

//#link: https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
func main() {

}

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func flatten(root *TreeNode) {
	treeNodeValS := preorderTraversal(root)
	for i := 1; i < len(treeNodeValS); i++ {
		pre, curr := treeNodeValS[i-1], treeNodeValS[i]
		pre.Left, pre.Right = nil, curr
	}
}

func preorderTraversal(root *TreeNode) []*TreeNode {
	list := make([]*TreeNode, 0)
	if root != nil {
		list = append(list, root)
		list = append(list, preorderTraversal(root.Left)...)
		list = append(list, preorderTraversal(root.Right)...)
	}
	return list
}
